import 'package:flutter/material.dart';
import 'package:flutter_demo/models/message.dart';
import 'package:flutter_demo/services/injection.dart';
import 'package:flutter_demo/states/message_state.dart';
import 'package:flutter_demo/widgets/ChatMessageList.dart';
import 'package:flutter_demo/widgets/UserInputWidget.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../states/chat_ui_state.dart';
import 'MessageItem.dart';

class ChatScreen extends HookConsumerWidget {

  final _textController = TextEditingController();
  final List<Message> messages = [
    Message(content: "Hello", isUser: true, timestamp: DateTime.now(), id: ''),
    Message(content: "How are you?", isUser: false, timestamp: DateTime.now(), id: ''),
    Message(
        content: "Fine,Thank you. And you?",
        isUser: true,
        timestamp: DateTime.now(), id: ''),
    Message(content: "I am fine.", isUser: false, timestamp: DateTime.now(), id: ''),
  ];
  ChatScreen({super.key});

  _requestChatGPT(WidgetRef ref, String content) async {
    ref.read(chatUiProvider.notifier).setRequestLoading(true);
    final message = Message(content: content, isUser: false, timestamp: DateTime.now(), id: '');
    ref.read(messageProvider.notifier).addMessage(message);
     // try {
     //   ref.read(chatUiProvider.notifier).setRequestLoading(true);
     //   final res = await chatgpt.sendChat(content);
     //   final text = res.choices.first.message?.content ?? "";
     //   final message = Message(content: text, isUser: false, timestamp: DateTime.now(), id: '');
     //   ref.read(messageProvider.notifier).addMessage(message);
     // } catch (e) {
     //   print(e);
     //   // logger.e()
     // } finally {
       ref.read(chatUiProvider.notifier).setRequestLoading(false);
     // }

  }

   _sendMessage(WidgetRef ref, String content) {
        _requestChatGPT(ref, content);
      // final message = Message(content: content, isUser: true, timestamp: DateTime.now());
      // // messages.add(message);
      // ref.read(messageProvider.notifier).addMessage(message);
      // _textController.clear();
   }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
     final messages = ref.watch(messageProvider);
     final chatUIState = ref.watch(chatUiProvider);
     return Scaffold(
      appBar: AppBar(
        title: const Text('Chat'),
      ),
      body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Expanded(
                  child: ChatMessageList()
              ),
              UserInputWidget()
              // TextField(
              //   controller: _textController,
              //   decoration: InputDecoration(
              //     hintText: 'Type a message',
              //     enabled: !chatUIState.requestLoading,
              //     suffixIcon: IconButton(
              //         onPressed: () {
              //           if(_textController.text.isNotEmpty){
              //             _sendMessage(ref,_textController.text);
              //           }
              //         },
              //         icon: const Icon(Icons.send))
              //   ),
              // )
            ],
          ),
      )
    );
  }
}
